<?php
/**
* 景区提现的订单
*/
class Withdraw_orders_model extends CI_Model
{
    private $_table  = 'withdraw_orders';
    private $_table1 = 'withdraw';

    /**
     * 总页数
     */
    public function total($params)
    {
        $params = array_map('trimArray', $params);
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'withdraw_orders.withdraw_id = withdraw.withdraw_id', 'INNER');

        if ( ! empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", $params['withdraw_id']);
        }
        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("created_at >=", $params['start_date']. ' 00:00:01');
        }
        if ( ! empty($params['end_date'])) {
            $this->db->where("created_at <=", $params['end_date']. ' 23:59:59');
        }
        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }

        return $this->db->count_all_results();
    }

    /**
     * 查询提现记录
     */
    public function page_list($params, $page_num, $num)
    {
        $params = array_map('trimArray', $params);
        $this->db->select('withdraw.withdraw_id, sid, scenery_name, uid, user_name, order_id, profit_amount, withdraw_orders.created_at');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'withdraw_orders.withdraw_id = withdraw.withdraw_id', 'INNER');

        if ( ! empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", $params['withdraw_id']);
        }

        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("created_at >=", $params['start_date']. ' 00:00:01');
        }
        if ( ! empty($params['end_date'])) {
            $this->db->where("created_at <=", $params['end_date']. ' 23:59:59');
        }
        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }

        $this->db->order_by('withdraw_orders.withdraw_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 批量插入
     */
    public function insertBatch($parameter)
    {
        if (empty($parameter)) {
            return false;
        }
        $data = array();
        foreach ($parameter as $key => $value) {
            foreach ($value as $val) {
                $data[] = array(
                    'order_id'      => $val['order_id'],
                    'withdraw_id'   => $key,
                    'profit_amount' => $val['amount'],
                    'status'        => 0,
                    'created_at'    => date('Y-m-d H:i:s')
                );
            }
        }
        $this->db->insert_batch($this->_table, $data);
        return $this->db->affected_rows(); //受影响行数
    }

    /**
     * 根据提现编号 查询提现订单
     */
    public function findByWithdrawIds($withdrawIds)
    {
        $this->db->select('withdraw.withdraw_id, order_id, profit_amount AS amount, withdraw.uid, withdraw.scenery_name');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'withdraw_orders.withdraw_id = withdraw.withdraw_id', 'INNER');
        $this->db->where_in('withdraw.withdraw_id', $withdrawIds);

        return $this->db->get();
    }
}